package clue_dao

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Database/admin/user_db"
	"hft/Http/Services/new_clue/clue_model"
	"hft/e"
	"hft/global"
)

// 找出活动
func FindActivity(code int, is_lou_pan_activity bool) (data *clue_model.ActivityModel, err error) {
	var newData clue_model.ActivityModel
	if !is_lou_pan_activity {
		var activity *admin.CommonActivity
		if err = global.OC_DB.Where("code = ?", code).First(&activity).Error; err != nil {
			return nil, errors.New(e.ActivityNotFound)
		} else {
			newData.Title = activity.Title
			return &newData, nil
		}
	} else {
		var activity *admin.Activity
		if err = global.OC_DB.Where("id = ?", code).First(&activity).Error; err != nil {
			return nil, errors.New(e.ActivityNotFound)
		} else {
			newData.Title = activity.Title
			return &newData, nil
		}
	}
}

// 生成单挑线索
func GenerateSingleClue(info *clue_model.ActivityClue, IP string, title string, port int, regionName string, louName string) (data *user_db.Clue) {
	return &user_db.Clue{
		ActivityName: title,
		LouName:      louName,
		OriginType:   1,
		Port:         port,
		IP:           IP,
		LouPanID:     info.LouID,
		ActivityCode: info.ActivityCode,
		RegionName:   regionName,
		HelpSelect: user_db.HelpSelect{
			DeliveryType: info.HelpSelect.DeliveryType,
			LouType:      info.HelpSelect.LouType,
			RoomType:     info.HelpSelect.RoomType,
			Area:         info.HelpSelect.Area,
			Furnish:      info.HelpSelect.Furnish,
			Objective:    info.HelpSelect.Objective,
			RegionName:   info.HelpSelect.RegionName,
			Budget:       info.HelpSelect.Budget,
		},
		Reservation: user_db.Reservation{
			Address:         info.Reservation.Address,
			LookType:        info.Reservation.LookType,
			ReservationTime: info.Reservation.ReservationTime,
			LatLng:          info.Reservation.LatLng,
			Adult:           info.Reservation.Adult,
			Children:        info.Reservation.Children,
			Baby:            info.Reservation.Baby,
		},
	}
}

// 生成活动线索
func GenerateActivityClue(list []*user_db.Clue, info *clue_model.ActivityClue, IP string, port int, regionName string, louName string) (newList []*user_db.Clue, err error) {
	if activity, err := FindActivity(info.ActivityCode, info.IsLouPanActivity); err != nil {
		return nil, err
	} else {
		list = append(list, GenerateSingleClue(info, IP, activity.Title, port, regionName, louName))
		return list, nil
	}
}

// 活动人数加一
func AddActivityNum(activityID int) (err error) {
	fmt.Println("hahahahha", activityID)
	if activityID != 0 {
		if res := global.OC_DB.Exec(fmt.Sprintf("UPDATE hft_activity SET application = application + 1 WHERE id = %v", activityID)); res.RowsAffected == 0 || res.Error != nil {
			fmt.Println("2", res)
			return errors.New(e.ActivityNotFound)
		} else {
			fmt.Println("3", res)
			return nil
		}
	} else {
		return nil
	}

}
